import 'package:bruno/bruno.dart';
import 'package:flustars/flustars.dart';
import 'package:flutter/material.dart';
import 'package:webview_flutter/webview_flutter.dart';
import 'package:xiongmao_clean_flutter_module/res/constant.dart';
import 'package:xiongmao_clean_flutter_module/util/common_utils.dart';

class WebPage extends StatefulWidget {
  String url;
  WebPage({this.url = "https://media.jiazhengye.cn/pdf/20240104006389-policy-65964acda8c86.pdf"});

  @override
  _WebPageState createState() => _WebPageState();
}

class _WebPageState extends State<WebPage> {
  late WebViewController controller;

  String _title = "详情";


  @override
  void initState() {
    super.initState();
    controller = WebViewController()
      ..setJavaScriptMode(JavaScriptMode.unrestricted)
      ..loadRequest(Uri.parse(widget.url));

    // 监听页面加载完成获取标题
    controller.setNavigationDelegate(
      NavigationDelegate(
        onPageFinished: (url) {
          controller.getTitle().then((title) {
            setState(() {
              _title = title?? "详情";
            });
          });

          // var string = SpUtil.getString(Constant.UserAgent);
          // if(string==null||string==""){
          //   controller.runJavaScriptReturningResult('navigator.userAgent').then((userAgent){
          //     print('User Agent--------:'+userAgent.toString().substring(1,userAgent.toString().length-1));
          //     SpUtil.putString(Constant.UserAgent, userAgent.toString().substring(1,userAgent.toString().length-1));
          //   });
          // }
        },
      ),
    );
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: BrnAppBar(
        title: _title,
      ),
      body: WebViewWidget(controller: controller,),
    );
  }
}
